unit clsObsPedCompras;

interface

uses Dm, dbtables, biblio, Sysutils;

type
  TObsPedCompras = class(Tobject)
  private
    Pedc_numero: string;
    Ent_codigo: string;
  public
    ObsPedc_1: string;
    ObsPedc_2: string;
    ObsPedc_3: string;
    ObsPedc_4: string;
    ObsPedc_5: string;
    function Gravar(p_Pedc_numero, p_Ent_codigo: string): Byte;
    function Recuperar(p_Pedc_numero, p_Ent_codigo: string): Byte;
    function Excluir(p_Pedc_numero, p_Ent_codigo: string): Byte;
  end;

implementation

{ TObsPedCompras }

function TObsPedCompras.Excluir(p_Pedc_numero, p_Ent_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM ObsPedCompra WHERE Pedc_numero=:k_Pedc_numero and Ent_codigo=:k_Ent_codigo');
  tq.parambyname('k_Pedc_numero').asstring := strzero(p_Pedc_numero, 9);
  tq.parambyname('k_Ent_codigo').asstring := strzero(p_Ent_codigo, 5);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

function TObsPedCompras.Gravar(p_Pedc_numero, p_Ent_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM ObsPedCompra where Pedc_numero=:k_Pedc_numero and Ent_codigo=:k_Ent_codigo');
  tq.parambyname('k_Pedc_numero').asstring := strzero(p_Pedc_numero, 9);
  tq.parambyname('k_Ent_codigo').asstring := strzero(p_Ent_codigo, 5);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO ObsPedCompra VALUES(' +
      ':k_Pedc_numero,' +
      ':k_Ent_codigo,' +
      ':k_Obspedc_1,' +
      ':k_Obspedc_2,' +
      ':k_Obspedc_3,' +
      ':k_Obspedc_4,' +
      ':k_Obspedc_5)');
  end
  else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE ObsPedCompra SET ' +
      'Obspedc_1 =:k_Obspedc_1,' +
      'Obspedc_2 =:k_Obspedc_2,' +
      'Obspedc_3 =:k_Obspedc_3,' +
      'Obspedc_4 =:k_Obspedc_4,' +
      'Obspedc_5 =:k_Obspedc_5 ' +
      'WHERE Pedc_numero=:k_Pedc_numero and Ent_codigo=:k_Ent_codigo');
  end;
  tq.parambyname('k_Pedc_numero').asstring := strzero(p_Pedc_numero, 9);
  tq.parambyname('k_Ent_codigo').asstring := strzero(p_Ent_codigo, 5);
  tq.parambyname('k_Obspedc_1').asstring := ObsPedc_1;
  tq.parambyname('k_Obspedc_2').asstring := ObsPedc_2;
  tq.parambyname('k_Obspedc_3').asstring := ObsPedc_3;
  tq.parambyname('k_Obspedc_4').asstring := ObsPedc_4;
  tq.parambyname('k_Obspedc_5').asstring := ObsPedc_5;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TObsPedCompras.Recuperar(p_Pedc_numero,
  p_Ent_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM ObsPedCompra where Pedc_numero=:k_Pedc_numero and Ent_codigo=:k_Ent_codigo');
  tq.Params[0].asstring := strzero(p_Pedc_numero, 9);
  tq.params[1].asstring := strzero(p_Ent_codigo, 5);
  tq.open;
  Pedc_numero := p_Pedc_numero;
  Ent_codigo := p_Ent_codigo;
  if tq.eof then begin
    ObsPedc_1 := '';
    ObsPedc_2 := '';
    ObsPedc_3 := '';
    ObsPedc_4 := '';
    ObsPedc_5 := '';
    Result := 0;
  end
  else begin
    Obspedc_1 := tq.fieldbyname('Obspedc_1').asstring;
    Obspedc_2 := tq.fieldbyname('Obspedc_2').asstring;
    Obspedc_3 := tq.fieldbyname('Obspedc_3').asstring;
    Obspedc_4 := tq.fieldbyname('Obspedc_4').asstring;
    Obspedc_5 := tq.fieldbyname('Obspedc_5').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;
end.
